package com.ztom.v2;

/**
 * @author ZhangTao
 */
public class Code100FindTheDuplicateNumber {

    public int findDuplicate(int[] nums) {
        // 同链表快慢指针
        // 数组 index 范围 [0, n], 数值范围 [1, n], 因此可以把 value 作为指针
        int slow = nums[0];
        int fast = nums[nums[0]];
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[nums[fast]];
        }
        fast = 0;
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[fast];
        }
        return slow;
    }
}
